home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / usrgrp32.fr_ / usrgrp32.fr
Text File  |  1995-09-04  |  6KB  |  216 lines

  1. VERSION 4.00
  2. Begin VB.Form frmMain 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Add User to Group"
  5.    ClientHeight    =   2100
  6.    ClientLeft      =   1890
  7.    ClientTop       =   2055
  8.    ClientWidth     =   4770
  9.    BeginProperty Font 
  10.       name            =   "MS Sans Serif"
  11.       charset         =   0
  12.       weight          =   700
  13.       size            =   8.25
  14.       underline       =   0   'False
  15.       italic          =   0   'False
  16.       strikethrough   =   0   'False
  17.    EndProperty
  18.    Height          =   2505
  19.    Left            =   1830
  20.    LinkTopic       =   "Form1"
  21.    ScaleHeight     =   2100
  22.    ScaleWidth      =   4770
  23.    Top             =   1710
  24.    Width           =   4890
  25.    Begin VB.CommandButton cmdShowUsers 
  26.       Caption         =   "&Show Users"
  27.       Enabled         =   0   'False
  28.       Height          =   375
  29.       Left            =   3240
  30.       TabIndex        =   6
  31.       Top             =   600
  32.       Width           =   1215
  33.    End
  34.    Begin VB.ComboBox cboGroups 
  35.       Height          =   300
  36.       Left            =   960
  37.       Sorted          =   -1  'True
  38.       Style           =   2  'Dropdown List
  39.       TabIndex        =   4
  40.       Top             =   600
  41.       Width           =   2115
  42.    End
  43.    Begin VB.ComboBox cboUsers 
  44.       Height          =   300
  45.       Left            =   990
  46.       Sorted          =   -1  'True
  47.       Style           =   2  'Dropdown List
  48.       TabIndex        =   2
  49.       Top             =   120
  50.       Width           =   2115
  51.    End
  52.    Begin VB.CommandButton cmdClose 
  53.       Cancel          =   -1  'True
  54.       Caption         =   "Cl&ose"
  55.       Height          =   555
  56.       Left            =   2520
  57.       TabIndex        =   1
  58.       Top             =   1200
  59.       Width           =   1755
  60.    End
  61.    Begin VB.CommandButton cmdAddUser 
  62.       Caption         =   "&Add User"
  63.       Default         =   -1  'True
  64.       Enabled         =   0   'False
  65.       Height          =   555
  66.       Left            =   480
  67.       TabIndex        =   0
  68.       Top             =   1200
  69.       Width           =   1755
  70.    End
  71.    Begin VB.Label Label1 
  72.       Alignment       =   1  'Right Justify
  73.       AutoSize        =   -1  'True
  74.       BackColor       =   &H00C0C0C0&
  75.       Caption         =   "&Group"
  76.       Height          =   195
  77.       Left            =   180
  78.       TabIndex        =   5
  79.       Top             =   660
  80.       Width           =   525
  81.    End
  82.    Begin VB.Label Label2 
  83.       Alignment       =   1  'Right Justify
  84.       AutoSize        =   -1  'True
  85.       BackColor       =   &H00C0C0C0&
  86.       Caption         =   "&User:"
  87.       Height          =   195
  88.       Left            =   240
  89.       TabIndex        =   3
  90.       Top             =   180
  91.       Width           =   465
  92.    End
  93. End
  94. Attribute VB_Name = "frmMain"
  95. Attribute VB_Creatable = False
  96. Attribute VB_Exposed = False
  97. Option Explicit
  98.  
  99. Private Declare Function GetPrivateProfileString _
  100.     Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal lpSectionName As String, _
  101.     ByVal lpKeyName As Any, ByVal lpDefault As String, _
  102.     ByVal lpReturnedString As String, ByVal Size As Integer, _
  103.     ByVal lpFileName As String) As Integer
  104.  
  105. Private Sub cboGroups_Click()
  106.     cmdAddUser.Enabled = IIf(cboUsers.ListIndex = -1 Or cboGroups.ListIndex = -1, False, True)
  107.     cmdShowUsers.Enabled = IIf(cboGroups.ListIndex = -1, False, True)
  108. End Sub
  109. Private Sub cboUsers_Click()
  110.     cmdAddUser.Enabled = IIf(cboUsers.ListIndex = -1 Or cboGroups.ListIndex = -1, False, True)
  111. End Sub
  112.  
  113. Private Sub cmdShowUsers_Click()
  114.     frmUsers.Tag = cboGroups.Text
  115.     frmUsers.Show 1
  116. End Sub
  117.  
  118. Private Sub Form_Load()
  119.     Dim myUser As String, myPass As String
  120.     Dim winDir As String * 128
  121.     Dim dirLen As Integer
  122.     
  123.     On Error GoTo LoadError
  124.     
  125.      ' Set the user and passwords for initial login.
  126.     myUser = "Admin"
  127.     myPass = "theboss"
  128.     
  129.     ' read VBDBHT.INI to get the name of the system database,
  130.     ' then assign that name to the SystemDB property
  131.     DBEngine.SystemDB = GetSystemDatabase()
  132.  
  133.     ' log in
  134.     DBEngine.DefaultUser = myUser
  135.     DBEngine.DefaultPassword = myPass
  136.     
  137.     FillUserList
  138.     FillGroupList
  139.  
  140. Exit Sub
  141. LoadError:
  142.     MsgBox Err & " " & Error$
  143. End
  144.  
  145. End Sub
  146.  
  147. Private Sub cmdAddUser_Click()
  148.     Dim newGroup As Group
  149.     Dim thePID As String
  150.     Dim usr As User
  151.     
  152.     On Error GoTo ChangeError
  153.     
  154.     ' If the user has not selected both a user and a group, generate an error
  155.     If cboUsers.ListIndex = -1 Then Error 32765
  156.     If cboGroups.ListIndex = -1 Then Error 32764
  157.     
  158.     ' Add the user to the designated group.
  159.     Set usr = DBEngine.Workspaces(0).Groups(cboGroups.Text).CreateUser(cboUsers.Text)
  160.     DBEngine.Workspaces(0).Groups(cboGroups.Text).Users.Append usr
  161.     
  162.     ' No errors, so must have been successful.
  163.     MsgBox "User " & cboUsers.Text & " added to " & cboGroups.Text, vbInformation
  164. Exit Sub
  165.  
  166. ChangeError:
  167.     Dim msg As String
  168.     Select Case Err.Number
  169.         Case 3032
  170.             msg = "User " & cboUsers.Text & " already belongs to Group " & cboGroups.Text
  171.         Case 32765
  172.             msg = "You have not selected a user."
  173.         Case 32764
  174.             msg = "You have not selected a group."
  175.         Case Else
  176.             msg = Err.Description
  177.     End Select
  178.     MsgBox msg, vbExclamation
  179. End Sub
  180.  
  181. Sub FillUserList()
  182.     Dim usr As User
  183.  
  184.     For Each usr In DBEngine.Workspaces(0).Users
  185.         If UCase$(usr.Name) <> "CREATOR" And UCase$(usr.Name) <> "ENGINE" Then
  186.             cboUsers.AddItem usr.Name
  187.         End If
  188.     Next
  189. End Sub
  190. Sub FillGroupList()
  191.     Dim grp As Group
  192.  
  193.     For Each grp In DBEngine.Workspaces(0).Groups
  194.         cboGroups.AddItem grp.Name
  195.     Next
  196. End Sub
  197. Private Sub cmdClose_Click()
  198.     End
  199. End Sub
  200.  
  201. Private Function GetSystemDatabase() As String
  202.     ' Returns the name of the system directory
  203.     
  204.     Const INI_FILENAME = "VBDBHT.INI"
  205.     Const MAX_PATH = 128
  206.  
  207.     Dim lpReturnedString As String * MAX_PATH
  208.     Dim bytesBack As Integer
  209.     
  210.     bytesBack = GetPrivateProfileString("Options", _
  211.         "SystemDB", "", lpReturnedString, MAX_PATH, INI_FILENAME)
  212.     GetSystemDatabase = IIf(bytesBack > 0, Left$(lpReturnedString, bytesBack), "")
  213.     
  214. End Function
  215.  
  216.